<?php

namespace Portal\Api;

use Portal\Common\DataApi as Api;
use Portal\Domain\Dept as DeptDomain;
use PhalApi\Exception\BadRequestException;

// use PhalApi\Api;

/**
 *日志接口
 * @author chenwengen 20240420
 */
class Log extends Api
{
    public function getRules()
    {
        return array(
         'tableList' => array(
                 'page' => array('name' => 'pageNum', 'type' => 'int', 'default' => 1, 'min' => 1, 'desc' => '页码'),
                'limit' => array('name' => 'pageSize', 'type' => 'int', 'default' => 10, 'min' => 1, 'max' => 100, 'desc' => '每页记录数'),
                'keywords' => array('name' => 'keywords','require' => false,'desc' => '字典类型'),
                'createTime' => array('name' => 'createTime', 'type' => 'array','require' => false, 'desc' => '操作时间范围'),
            ),
            // 'deleteData' => array(
            //     'ids' => array('name' => 'id', 'require' => true, 'type' => 'int', 'min' => 1, 'desc' => \PhalApi\T('ID to delete')),
            // ),
            // 'deleteDataIDs' => array(
            //     'ids' => array('name' => 'ids', 'type' => 'array', 'format' => 'explode', 'seperator' => ',', 'require' => true, 'desc' => \PhalApi\T('ids to delete'))
            // ),
            'visitTrend' => array(
                'startDate' => array('name' => 'startDate','type' => 'date', 'format' => 'timestamp',  'require' => true, 'desc' => '开始时间'),
                'endDate' => array('name' => 'endDate', 'type' => 'date', 'format' => 'timestamp',  'require' => true,  'desc' => '结束时间'),
            ),
            'visitStats' => array(

            ),
        );
    }
    protected function getDataModel()
    {
        return  new \Portal\Model\Log();
    }
    /**
     * 获取表格列表数据
     * @desc 通用数据接口，获取表格列表数据，默认按ID降序返回，支持分页和搜索
     */
    public function tableList()
    {
        $model = $this->getDataModel();

        // $searchParams = array();
        // foreach ($this->searchParams as $key => $val) {
        //     if ($val !==  '') {
        //         $searchParams[$key] = $val;
        //     }
        // }

        // // 条件
        // $where = !empty($searchParams) ? $searchParams : null;
        // $whereParams = array();
        // $where = $this->getTableListWhere($where);
        $searchParams = array();
        // 条件
        $where = !empty($searchParams) ? $searchParams : null;
        $whereParams = array();
        $where['is_deleted'] = 0;
        $where = $this->getTableListWhere($where);
        $select = $this->getTableListSelect();
        $order = $this->getTableListOrder();
        $page = $this->page;
        $perpage = $this->limit;

        $total = $model->count($where);
        $items = $total > 0 ? $model->getList($where, $whereParams, $select, $order, $page, $perpage) : array();
        $items = $this->afterTableList($items);

        return $this->returnDataResult(array('total' => $total, 'list' => $items));
    }

    // 列表返回的字段
    protected function getTableListSelect()
    {
        return 'id,module,content,request_uri AS requestUri,method,ip,province,city,browser,os,execution_time AS executionTime,create_by as createBy,create_time AS createTime';
    }

    // 列表的默认排序
    protected function getTableListOrder()
    {
        return 'id DESC';
    }

    // 查询条件
    protected function getTableListWhere($where)
    {
        if ($this->keywords !== null && trim($this->keywords) != '') {
            $where['content  LIKE ?'] = array("%$this->keywords%");
        }
        if (isset($this->createTime[0]) && trim($this->createTime[0]) != '') {
            $where['create_time BETWEEN ? AND ?'] = array(strtotime($this->createTime[0]), strtotime($this->createTime[1]));
        }

        return $where;
    }

    // 取到列表数据后的加工处理
    protected function afterTableList($items)
    {
        $domainAdmin = new \Portal\Domain\Admin();
        foreach ($items as $key => $item) {
            if (isset($item['createTime'])) {
                $item['createTime'] = date('Y-m-d H:i:s', $item['createTime']);
            }
            if (isset($item['province']) && isset($item['city'])) {
                $item['region'] = strval($item['province']).' '.strval($item['city']);
                unset($item['province'],$item['city']);
            }
            if (isset($item['createBy'])) {
                $item['operator'] = strval($domainAdmin->getUserName($item['createBy']));
            }
            $items[$key] = $item;
        }
        return $items;
    }

    /**
     * 获取访问趋势
     * @desc 获取访问趋势
     * @return object
     * @exception 403 请重新登陆
     */
    public function visitTrend()
    {
        $startDate = $this->startDate;
        $endDate = $this->endDate;
        $returnData['dates'] = [
          "2024-06-30",
          "2024-07-01",
          "2024-07-02",
          "2024-07-03",
          "2024-07-04",
          "2024-07-05",
          "2024-07-06",
          "2024-07-07",
        ];
        $returnData['pvList'] = [1751, 5168, 4882, 5301, 4721, 4885, 1901, 1003];
        $returnData['uvList'] = null;
        $returnData['ipList'] = [207, 566, 565, 631, 579, 496, 222, 152];
        return $returnData;
    }
    /**
     * 获取今日访问统计
     * @desc 获取今日访问统计
     * @return object
     * @exception 403 请重新登陆
     */
    public function visitStats()
    {
        $returnData['todayPvCount'] = 1629;
        $returnData['totalPvCount'] = 286086;
        $returnData['pvGrowthRate'] = -0.65;
        $returnData['todayIpCount'] = 169;
        $returnData['totalIpCount'] = 19985;
        $returnData['ipGrowthRate'] = -0.57;
        return $returnData;
    }

}
